import mysql.connector

class memory:
    def __init__(self):
        #init mysql connection to using
        self.mydb = mysql.connector.connect(
        host="localhost",
        user="phpmyadmin",
        passwd="henjiandan",
        database="robot",
        buffered="true"
        )
#update the code you tryed from position to mysql
    def update_code(self,codeT):
        mycursor = self.mydb.cursor()
        #mycursor.execute("SELECT* FROM `code-try`;")
        mycursor.execute("UPDATE `code-try` SET `code`='{}' WHERE `position` = 'first';".format(codeT))
        self.mydb.commit()
        print(mycursor.rowcount)
        #data = mycursor.fetchall()
        #print(data)
    def use_code(self,position):
        mycursor = self.mydb.cursor()
        mycursor.execute("SELECT `code` FROM `code-try` WHERE `position`='{}'".format(position))
        myresult = mycursor.fetchone()
        #for x in myresult:
        #   codeT = x
        return myresult

    def getIntent(self,response):
        mycursor =  self.mydb.cursor()
        mycursor.execute("SELECT `responses` FROM `intents` WHERE `tag`='{}'".format(response))
        myresult = mycursor.fetchone()
        return myresult